/* Copyright 2017, 2018, 2019, 2020 Gabriel Czernikier
 *
 * This file is part of Côtehaus.
 * Côtehaus is free software: you can redistribute it and/or modify
 * it under the terms of one of the following:
 * 1. The same dual license as that of Côtehaus itself, of which individual licenses are
 * the MIT License and the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 2. The MIT License.
 * 3. The GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Côtehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * MIT License and the GNU General Public License for more details.
 * You should have received a copy of the MIT License and the GNU General Public License
 * along with Côtehaus.  If not, see <https://opensource.org/licenses/MIT> and <http://www.gnu.org/licenses/>.
 */

#define _BSD_SOURCE
#include <endian.h>
#include <iconv.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "types.h"

void pack_ubyte_array(void *(*write_fnc)(void *src, size_t n), struct size_data *for_pack) {
	write_fnc(for_pack->data, for_pack->size);
}

void pack_uint16_t(void *(*write_fnc)(void *src, size_t n), struct size_data *for_pack) {
	write_fnc(htole16(for_pack->data), for_pack->size);
}

void pack_uint32_t(void *(*write_fnc)(void *src, size_t n), struct size_data *for_pack) {
	write_fnc(htole32(for_pack->data), for_pack->size);
}


struct size_data *for_ubyte_array(struct size_data **sd, size_t n){
	*sd = malloc(sizeof(struct size_data));
	(*sd)->size = n*sizeof(char);
	(*sd)->data = malloc(n*sizeof(char));
	(*sd)->data_pack_fnc = pack_ubyte_array;
	return *sd;
}
struct size_data *for_uint16_t(struct size_data **sd){
	*sd = malloc(sizeof(struct size_data));
	(*sd)->size = sizeof(uint16_t);
	(*sd)->data = malloc(sizeof(uint16_t));
	(*sd)->data_pack_fnc = pack_uint16_t;
	return *sd;
}
struct size_data *for_uint32_t(struct size_data **sd){
	*sd = malloc(sizeof(struct size_data));
	(*sd)->size = sizeof(uint32_t);
	(*sd)->data = malloc(sizeof(uint32_t));
	(*sd)->data_pack_fnc = pack_uint32_t;
	return *sd;
}
